﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HtmlPictureHelper
{
    public partial class SettingForm : Form
    {
        public SettingForm()
        {
            InitializeComponent();
        }

        private void SettingForm_Load(object sender, EventArgs e)
        {
            btnReset_Click(sender, e);
        }

        /// <summary>
        /// 检查控件数据
        /// </summary>
        /// <param name="control"></param>
        /// <param name="checkCallback"></param>
        /// <param name="errorMessage"></param>
        /// <returns></returns>
        private int CheckError(Control control,Func<bool> checkCallback,string errorMessage)
        {
            bool? result = checkCallback?.Invoke();
            int errorFlag = 0;
            if(result !=true)
            {
                errorProvider1.SetError(control, errorMessage);
                errorFlag = 1;
            }
            return errorFlag;
        }

        /// <summary>
        /// 获取校验后的配置模型
        /// </summary>
        /// <returns></returns>
        private Model.AppConfiguration GetAppConfigurationModel()
        {
            int errorCount = 0;
            errorCount += CheckError(this.txtPictureCaptionExpression, () => { return !string.IsNullOrEmpty(this.txtPictureCaptionExpression.Text); }, "图片标题表达式不能为空");

            string[] splitedExpression = null;
            errorCount += CheckError(this.txtPictureCountExpression, () => 
            {
                if(string.IsNullOrEmpty(this.txtPictureCountExpression.Text))
                {
                    return false;
                }
                try
                {
                    splitedExpression = this.txtPictureCountExpression.Text.Split(new char[] { ',' });
                    if(splitedExpression == null || splitedExpression.Length <=0)
                    {
                        return false;
                    }
                    return true;

                }
                catch (Exception)
                {

                    return false;
                }
            }, "图片数量（新）表达式不能为空");
          
            errorCount += CheckError(this.txtSinglePictureLinkExpression, () => { return !string.IsNullOrEmpty(this.txtSinglePictureLinkExpression.Text); }, "单个图片链接表达式不能为空");
            errorCount += CheckError(this.numWebClientCount, () => { return numWebClientCount.Value>0; }, "WebClient实例数量必须大于0");
            if(errorCount !=0)
            {
                return null;
            }
            return new Model.AppConfiguration()
            {
                PictureCaptionExpression = this.txtPictureCaptionExpression.Text,
                PictureCountExpressions = this.txtPictureCountExpression.Text,
                PictureSigleLink = this.txtSinglePictureLinkExpression.Text,
                WebClientInstanceCount = (int)this.numWebClientCount.Value
            };

        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            Model.AppConfiguration model = GetAppConfigurationModel();
            if(model != null)
            {
                App.Default.WebClientInstanceCount = model.WebClientInstanceCount;
                App.Default.PictureCaptionExpression = model.PictureCaptionExpression;
                App.Default.PictureCountExpressions = model.PictureCountExpressions;
                App.Default.PictureSingleLinkExpression = model.PictureSigleLink;
                App.Default.Save();
                btnCancle_Click(sender, e);
            }
            
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            this.numWebClientCount.Value = App.Default.WebClientInstanceCount;
            this.txtPictureCaptionExpression.Text = App.Default.PictureCaptionExpression;
            this.txtPictureCountExpression.Text = App.Default.PictureCountExpressions;
            this.txtSinglePictureLinkExpression.Text = App.Default.PictureSingleLinkExpression;
            this.errorProvider1.Clear();
        }

        private void txtPictureCountExpressionNew_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
